<?php
/**
 * Created by PhpStorm.
 * User: lixy
 * Date: 2015/12/21
 * Time: 20:29
 */

namespace app\controllers;


use app\lib\Tools;
use app\models\weixin\T_WXMALL_SCENE_USER;
use app\models\weixin\T_WXMALL_USER;
use Yii;
use app\lib\WeixinUtils;
use app\lib\WXBaseController;
use app\lib\WebService;
use app\models\weixin\T_WXMALL_DORACARD;
use app\models\BizDao;
use app\models\T_WX_PAY_LOG;
use app\lib\RandCheckCode;
use app\lib\wxpay\WxPayUnifiedOrder;
use app\lib\wxpay\WxPayTools;
use app\lib\wxpay\WxPayApi;
use app\models\BagDao;
use app\models\HomeDao;


//wx-biz/
class WxBizController extends WXBaseController
{


    public function beforeAction($action)
    {

        $userinfo = WeixinUtils::weixin_userinfo_filter();
//         var_dump($userinfo );
//        die;

        if ($userinfo == null) {
            echo "验证失败";
            return false;
        }
//        echo $userinfo->userid;

        return true;
    }

    public function actionIndex()
    {
        return $this->redirect(["wx-biz/card-list"]);

    }

    //卡片列表
    public function actionCardList()
    {
        $session = Yii::$app->session;
        $openid = $session->get(static::sessionKey)->openid;
        if (T_WXMALL_DORACARD::get_User_Sale($openid) == 0) {
            $cards = T_WXMALL_DORACARD::get_available_Cards();
        } else {
            $cards = T_WXMALL_DORACARD::get_available_Cards_for_sale();
        }

        $banners=HomeDao::getCardListBanner();

        return $this->render("card-list", ["cards" => $cards, 'banners'=>$banners]);
    }

    //查看卡片产品 ps:为了合并到统一的支付路径下
    public function actionCardDetail($id, $isbuy = 1)
    {

        $session = Yii::$app->session;
        $card = T_WXMALL_DORACARD::get_cardByCardId($id);
        if (count($card) == 0) {
            echo "此卡片不存在！";
            exit;
        }
        $card = $card[0];
        $goods = T_WXMALL_DORACARD::get_goodsByCardId($id);
        foreach ($goods as &$good) {
            $good['GOODS_DETAIL_HTML'] = "";
        }

        //------begin是否是满减
        $openid = $session->get(static::sessionKey)->openid;
        //isMJ大于0 是满减
        $isMJ = T_WXMALL_SCENE_USER::getSCENE_USER_isValid_count($openid, '1');
        //-----end

//        var_dump($card);die;

        return $this->render("card-detail", ["goods" => $goods, "card" => $card, "isbuy" => $isbuy, 'isMJ' => $isMJ]);

    }

//json 返回产品信息和库存
    public function actionGetGoodHandle()
    {
        $request = Yii::$app->request;
        $isShowStock = $request->post("isShowStock");
        $CARD_ID = $request->post("card_id");
        $GOODCODE = $request->post("good_code");
        $msg = "";
        $stock = 0;
        $flag = 0;
        if ($isShowStock == 1) {
            //获取库存
            $stock = WebService::nc_getGiftStock($GOODCODE);
        }
        $good = T_WXMALL_DORACARD::get_goodByCardIdAndGoodId($CARD_ID, $GOODCODE);
        if (count($good) == 0) {
            $msg = "此产品不存在！";
        } else {
            $flag = 1;
            $good = $good[0];
        }
        echo json_encode(['msg' => $msg, 'flag' => $flag, 'good' => $good, 'stock' => $stock]);
    }

    // json返回产品库存
    public function actionGetGoodsStock()
    {
        $request = Yii::$app->request;
        $goodsCode = $request->post("goods_code");
        $msg = "";
        $stock = 0;
        $stock = WebService::nc_getGiftStock($goodsCode);

        echo json_encode(['stock' => $stock]);
    }
    //校验订单并返回订单信息
    public static function ___verifyOrder($code, $user_id)
    {

        $orderInfo = T_WXMALL_DORACARD::getOrderInfoByOrderCode($code, $user_id);
        if (count($orderInfo) == 0) {
            echo "订单不存在";
            exit;
        }
        return $orderInfo = $orderInfo[0];
    }
    ////点击我的送礼记录跳转 查看订单导航  code 订单编号  view-order-nav
    public function actionViewOrderNav($order_code)
    {
        $session = Yii::$app->session;
        $user_id = $session->get(static::sessionKey)->userid;

        $orderInfo = static::___verifyOrder($order_code, $user_id);
        $STATUS = $orderInfo['STATUS'];
//        1：已购买未支付
//        2：已支付
//        3：已失效

        if ($STATUS == '1') {
            return $this->redirect(["wx-pay/wx-card-order-form", 'order_code' => $order_code]);
        } else {
            if ($STATUS == '2') {
                //已支付
                return $this->redirect(["wx-biz/gift-send-detail", 'id' => $order_code]);
                exit;
            } else {
                echo "已失效;STATUS:" . $STATUS;
                exit;
            }
        }
    }

    //生成卡片订单 create-card-order-handle
    public function actionCreateCardOrderHandle()
    {
        $msg = "";
        $flag = 0;
        $order_code = "";
        $session = Yii::$app->session;
        $request = Yii::$app->request;
        $cardid = $request->post("cardid");
        $count = $request->post("count");

        $userid = $session->get(static::sessionKey)->userid;
        $openid = $session->get(static::sessionKey)->openid;

        $card = T_WXMALL_DORACARD::get_cardByCardId($cardid);
        if (count($card) == 0) {
            $msg = "卡片不存在";
        } else {
            $card = $card[0];

            $price = floatval($card['PRICE']) * (int)$count * 100;
           /* //满100减50
            if (T_WXMALL_SCENE_USER::getSCENE_USER_isValid_count($openid, 1) > 0) {
                $attach = "满减";
                if ($price >= 100 * 100)
                    $price = $price - (50 * 100);
            }*/


            $order_code = T_WXMALL_DORACARD::create_order($cardid, $count, $price * 0.01, $userid);
            if ($order_code == "0") {
                $msg = "卡牌订单生成失败";
            } else {
                $flag=1;

            }
        }
        echo json_encode(['msg' => $msg, 'flag' => $flag, 'order_code' => $order_code]);
    }
    const defalut_blessing = '我在朵拉盒为您选了一份朵拉卡，祝您万事如意。';
    //购买卡片json pay-handle
    public function actionPayHandle()
    {
        $msg = "";
        $flag = 0;
        $session = Yii::$app->session;
        $request = Yii::$app->request;


        $order_code=$request->post("code");
        $current_coin = $request->post("current_coin");
        $BLESSING = $request->post("blessing");

        if (trim($BLESSING) == '') {
            $BLESSING = static::defalut_blessing;
        }
        $userid = $session->get(static::sessionKey)->userid;
        $openid = $session->get(static::sessionKey)->openid;
        $orderInfo = T_WXMALL_DORACARD::getOrderInfoByOrderCode($order_code, $userid);
        if (count($orderInfo) == 0) {
            $msg = '订单不存在';
            echo json_encode(['msg' => $msg, 'flag' => $flag]);
            exit;
        }
        $orderInfo = $orderInfo[0];
        //添加祝福语
        $re = T_WXMALL_DORACARD::add_order_BLESSING($order_code, $userid, $BLESSING);
        if ($re == 0) {
            echo json_encode(['msg' => '添加祝福语失败', 'flag' => 0,]);
            exit;
        }
        $card = T_WXMALL_DORACARD::get_cardByOrderId($order_code);
        if (count($card) == 0) {
            $msg = "卡片不存在";
        } else {

            $card = $card[0];

            //计算价格
            $count = $orderInfo['NUM'];
            $result = BizDao::P_WXMALL_CALC_PRICE($userid, $current_coin, $order_code);
            if ($result['RESULTS'] != 1) {
                echo json_encode(['msg' => '生成支付订单失败:' . $result['RESULTS'], 'flag' => 0,]);
                exit;
            }
            $price = $result['realPrice'];

            $price = floatval($price) * 100;


            //附加数据
            $attach = '';
            if ($current_coin != '0') {
                $attach = "current_coin:{$current_coin};";
            }
            //加入订单号
            $attach=$attach."order_no:{$order_code};";
            $random_order=Tools::build_order_no('card_');
            //测试用
            if (YII_ENV == 'dev' && $price > 0) {
                echo json_encode(['msg' => '测试单证产生完毕', 'flag' => 0, 'testcode' => $order_code, 'price' => $price]);
                exit;
            }
            if ($price > 0) {
                // //---------begin---------生成支付订单



                $input = new WxPayUnifiedOrder();
                $input->SetBody($card['NAME'] . "×$count");
                $input->SetOut_trade_no($random_order);
                $input->SetTotal_fee($price);
                $input->SetTime_start(date("YmdHis"));
                $input->SetTime_expire(date("YmdHis", time() + 600));
                $input->SetNotify_url(PAYURL);
                $input->SetTrade_type("JSAPI");
                $input->SetOpenid($openid);
                $input->SetAttach($attach);
                $order = WxPayApi::unifiedOrder($input);

                //return_code 和result_code
                if ($order['return_code'] == 'SUCCESS' && $order['result_code'] == 'SUCCESS') {
                    $prepay_id = $order['prepay_id'];
                    $jsApiParameters = WxPayTools::GetJsApiParameters($order);
                    //                    file_put_contents("d://111.txt",$jsApiParameters);
                    $flag = 1;  //生成成功
                    //设置优惠取消
                    // T_WXMALL_SCENE_USER::setSCENE_USER_invalid($openid,1);
                    //输出结果
                    echo json_encode(['msg' => $msg, 'flag' => $flag, 'code' => $order_code, 'jsApiParameters' => $jsApiParameters, 'price' => $price]);
                    exit;
                } else {
                    $msg = '下支付订单错误：' . $order['return_msg'].'---'.$order['err_code_des'];

                }
                ///---------------end
            }else {
                $re=T_WX_PAY_LOG::write_P_WXMALL_PAY_ORDER('SUCCESS', 'dev'
                    , $openid, 0, $random_order,'price is zero', date("YmdHis")
                    , $attach);

                if($re!='1'){
                    $flag = 0;
                    $msg = 'pay order error!'.$attach;
                    echo json_encode(['msg' => $msg, 'flag' => $flag, 'code' => $order_code]);
                    exit;
                }

                $flag = 1;  //生成成功
                echo json_encode(['msg' => $msg, 'flag' => $flag, 'code' => $order_code, 'price' => $price]);
                exit;
            }
        }


        echo json_encode(['msg' => $msg, 'flag' => $flag, 'code' => $order_code]);
    }


    //成功购买卡片
    public function actionPayCardSuccess()
    {
        $request = Yii::$app->request;
        $id = $request->get('id');//orderid

        $session = Yii::$app->session;
        $userid = $session->get(static::sessionKey)->userid;

        $user = T_WXMALL_USER::getUserByUserid($userid);
        $user = $user[0];
        return $this->render("pay-card-success", ['orderid' => $id, 'user' => $user]);
    }

    //抢卡片   order_id
    public function actionGetCard($id)
    {
        return $this->redirect(['wx-biz/gift-receive','id'=>$id]);
    }

    //根据用户和订单号判断跳转页面navigation  order_id id
    public function actionIsReceiveCard($id)
    {
        $session = Yii::$app->session;
        $userid = $session->get(static::sessionKey)->userid;
        $cardcodeid = T_WXMALL_DORACARD::isReceive_card($id, $userid);
        if ($cardcodeid != false) {
            //echo "已经领取";
            $this->actionExchangeCardNav($cardcodeid);
        }


    }

    //领卡片 json
    public function actionGetCardHandle()
    {
        $msg = "";
        $flag = 0;
        $session = Yii::$app->session;
        $request = Yii::$app->request;
        $orderid = $request->post("orderid");


        $userid = $session->get(static::sessionKey)->userid;
        $re = T_WXMALL_DORACARD::create_RECEIVE_CARD($orderid, $userid);
        if ($re != "1") {
            $msg = $re;//领取失败
        } else {
            $flag = 1;

            $cardcodeid = T_WXMALL_DORACARD::isReceive_card($orderid, $userid);
        }


        echo json_encode(['msg' => $msg, 'flag' => $flag, 'orderid' => $orderid,'card_code_id'=>isset($cardcodeid)?$cardcodeid:null]);
    }

    //查看卡片领取详情 orderid  WD_0000_2016_1048
    public function actionGetCardLog($id)
    {
        $session = Yii::$app->session;

        $card = T_WXMALL_DORACARD::get_cardByOrderId($id);
        if (count($card) == 0) {
            echo "此卡片不存在！";
            exit;
        }
        $card = $card[0];

        $count = T_WXMALL_DORACARD::get_orderCardCountByOrderId($id);
        $getcount = T_WXMALL_DORACARD::get_orderGetCardCountByOrderId($id);
        $rCards = T_WXMALL_DORACARD::getReceiveCardListByOrderId($id);

        $userid = $session->get(static::sessionKey)->userid;
        $user = T_WXMALL_USER::getUserByUserid($userid);
        $user = $user[0];
        return $this->render("get-card-log", ['card' => $card, 'rCards' => $rCards, 'count' => $count,
            'getcount' => $getcount, 'orderid' => $id, 'user' => $user]);

    }

    //查看送出的礼卡详情 orderid  WD_0000_2016_1048
    public function actionGiftSendDetail($id)
    {
        $session = Yii::$app->session;

        $card = T_WXMALL_DORACARD::get_cardByOrderId($id);
        if (count($card) == 0) {
            echo "此卡片不存在！";
            exit;
        }
        $card = $card[0];

        $count = T_WXMALL_DORACARD::get_orderCardCountByOrderId($id);
        $getcount = T_WXMALL_DORACARD::get_orderGetCardCountByOrderId($id);
        $rCards = T_WXMALL_DORACARD::getGetCardRecordListByOrderId($id);

        $userid = $session->get(static::sessionKey)->userid;
        $user = T_WXMALL_USER::getUserByUserid($userid);
        $user = $user[0];
        return $this->render("gift-send-detail", ['card' => $card, 'rCards' => $rCards, 'count' => $count,
            'getcount' => $getcount, 'orderid' => $id, 'user' => $user]);

    }

//card_code_id $id 返回卡片信息
    public function ___getCardInfo($card_code_id)
    {
        $re = (object)null;
        $card = T_WXMALL_DORACARD::get_cardByCardCodeId($card_code_id);
        if (count($card) == 0) {
            echo "此卡片不存在！";
            exit;
        }

        $re->card = $card[0];
        $re->orderid = $card[0]['ORDER_ID'];

        return $re;
    }

    //兑换卡片   card_code_id $id
    public function actionExchangeCard($id)
    {
        $info = $this->___getCardInfo($id);
        $orderid = $info->orderid;
        $count = T_WXMALL_DORACARD::get_orderCardCountByOrderId($orderid);
        $getcount = T_WXMALL_DORACARD::get_orderGetCardCountByOrderId($orderid);
        $rCards = T_WXMALL_DORACARD::getReceiveCardListByOrderId($orderid);
        return $this->render("exchange-card", ['card' => $info->card, 'rCards' => $rCards, 'count' => $count,
            'getcount' => $getcount, 'order_id' => $orderid, 'card_code_id' => $id,]);
    }

    //根据用户和订单号判断跳转页面navigation  card_code_id id
    public function actionExchangeCardNav($id)
    {
        $session = Yii::$app->session;
        $userid = $session->get(static::sessionKey)->userid;
        $status = T_WXMALL_DORACARD::getGetStatusByCardCodeId($id);
        if ($status == false) {
            echo "没有这个cardcodeid";
            exit;
        }
        if ($status == 1) {//未兑换
            return $this->redirect(["wx-biz/exchange-card", "id" => $id]);
        }
        if ($status == 2) {//已兑换
            return $this->redirect(["wx-biz/view-good-order", "id" => $id]);
        }
        if ($status == 3) {//已经过期
            echo "已经过期";
            exit;
        }
        if ($status == 4) {//已经失效
            echo "已经失效";
            exit;
        }


    }

    //挑选礼品   card_code_id $id
    public function actionGoodChose($id)
    {
        $info = $this->___getCardInfo($id);
        $cardid = $info->card['ID'];
        $goods = T_WXMALL_DORACARD::get_goodsByCardId($cardid);
        $doracoin = $this->__get_doracoin_info($id);
        if (count($doracoin) > 0) {
            $doracoin = $doracoin[0];
        }

        return $this->render("good-chose",
            ['card' => $info->card, 'goods' => $goods, 'card_code_id' => $id, 'doracoin' => $doracoin]);
    }

    public function __get_doracoin_info($card_code_id)
    {
        $doracoin = T_WXMALL_DORACARD::get_DoracoinInfo($card_code_id);
        return $doracoin;
    }

    //填写兑换礼品物流信息  card_code_id  $id
    public function actionConfirmGoodOrder($id, $good_code)
    {
        $info = $this->___getCardInfo($id);
        $CARD_ID = $info->card['ID'];
        $good = T_WXMALL_DORACARD::get_goodByCardIdAndGoodId($CARD_ID, $good_code);
        if (count($good) == 0) {
            echo "此产品不存在！";
            exit;
        }
        $good = $good[0];


        return $this->render("confirm-good-order",
            ['good' => $good, 'card_id' => $CARD_ID, 'card_code_id' => $id,]);
    }

    public function actionGetAreaHandle()
    {
        $request = Yii::$app->request;
        $pk = $request->post("pk");

        $msg = "";

        $flag = 1;
        if ($pk == null) {
            $others = T_WXMALL_DORACARD::get_AreaList_sheng();
        } else {
            $others = T_WXMALL_DORACARD::get_AreaList_other($pk);
        }

        echo json_encode(['msg' => $msg, 'flag' => $flag, 'others' => $others,]);
    }

    //json 提交订单 submit-order-handle
    public function actionSubmitOrderHandle()
    {

        $session = Yii::$app->session;
        $request = Yii::$app->request;


        $card_code_id = $request->post("card_code_id");
        //如果是离线的，写入user_id,收卡;
        if(T_WXMALL_DORACARD::isOfflineCard($card_code_id)){
            if(T_WXMALL_DORACARD::isReceive_card_offline($card_code_id)==false){
                $re=T_WXMALL_DORACARD::P_RECEIVE_CARD_OFFLINE($card_code_id,static::getSessionUserid());
                if($re!='1'){
                    echo $re;
                    exit;
                }

            }
        }


        $good_code = $request->post("good_code");

        $stock = WebService::nc_getGiftStock($good_code);
        if($stock<=0) {
            echo json_encode(['msg' => '库存不足', 'flag' => 0]);
            exit;
        }


        $RECEIVERNAME = $request->post("RECEIVERNAME");
        $RECEIVERPHONE = $request->post("RECEIVERPHONE");
        $RECEIVERADDRESS = $request->post("RECEIVERADDRESS");
        $CREMARK = $request->post("CREMARK");
        $sheng = $request->post("sheng");
        $shi = $request->post("shi");
        $qu = $request->post("qu");

        $msg = "";

        $userid = $session->get(static::sessionKey)->userid;

        $info = T_WXMALL_DORACARD::getPriceAndCardIdByCardcodeid($card_code_id);

        $price = $info[0]["PRICE"];

        $good = T_WXMALL_DORACARD::get_goodByCardIdAndGoodId($info[0]["ID"], $good_code);
        $goodname = $good[0]['GOODS_NAME'];

        //$card_code_id  做为订单编号用
        $flag = T_WXMALL_DORACARD::P_CARD_EXCHANGE($userid, $card_code_id, $good_code, $RECEIVERNAME, $RECEIVERPHONE, $RECEIVERADDRESS, $CREMARK, $sheng, $shi, $qu);


        if ($flag == 0) {
            $msg = "兑换失败";
        } else {
            /*public 'status' => string '0' (length=1)
              public 'info' => string '收货人不能为空!' (length=22)
              public 'data' => string '' (length=0)*/
            $re = WebService::nc_setWxOrder($card_code_id, $RECEIVERNAME,
                $sheng, $shi, $qu,
                $RECEIVERADDRESS, $RECEIVERPHONE, "", "", $price, $good_code, $goodname, "1", $price);
//            var_dump($re);
            T_WXMALL_DORACARD::setNC_SYNSTATUS($card_code_id, $re->status);

        }
        echo json_encode(['msg' => $msg, 'flag' => $flag]);
        exit;
    }

//    兑换礼品 下单成功
    public function actionGoodSubmitOrderSuccess()
    {
        return $this->render("good-submit-order-success");
    }


    //查看兑换商品的物流信息  card_code_id
    public function actionViewGoodOrder($id)
    {
        $info = $this->___getCardInfo($id);

        $good = T_WXMALL_DORACARD::get_GoodByCardCodeId($id);
        if (count($good) == 0) {
            echo "没有兑换的产品";
            exit;
        }
        $good = $good[0];

        return $this->render("view-good-order", ['good' => $good, 'card_code_id' => $id]);
    }


    //个人中心
    public function actionUserCenter()
    {
        $session = Yii::$app->session;
        $userinfo = $session->get(static::sessionKey);
        $user = T_WXMALL_USER::getUserByUserid($userinfo->userid);
        if (count($user) == 0) {
            $tel = '微信信息没有注册到系统';
        } else {
            $tel = $user[0]['PHONE'];
        }

        return $this->render("user-center", ['tel' => $tel]);
    }

    //发送绑定手机号码的验证码
    public function actionUserCenterSendCodeHandle()
    {
        $msg = "";
        $flag = 0;
        $session = Yii::$app->session;
        $request = Yii::$app->request;
        $tel = $request->post("tel");
        $code = RandCheckCode::get_code(5, 1);
        //$re=true;
        $re = WebService::sendsms($tel, $code);
        if ($re == false) {
            $msg = "发送验证码失败";
        } else {
            $flag = 1;
            $session->set('user-center-code', $code);
            $session->set('user-center-tel', $tel);
        }

        echo json_encode(['msg' => $msg, 'flag' => $flag,]);
    }

//发送绑定手机号码的验证码
    public function actionUserCenterSetTelHandle()
    {
        $msg = "";
        $flag = 0;
        $session = Yii::$app->session;
        $request = Yii::$app->request;
        $scode = $request->post("code");
        $code = $session->get('user-center-code');
        $tel = $session->get('user-center-tel');
        if ($scode != $code) {
            $msg = "验证码错误";
        } else {
            $userinfo = $session->get(static::sessionKey);
            $re = T_WXMALL_USER::set_user_tel($userinfo->userid, $tel);
            if ($re != 1) {
                $msg = "绑定失败";
            } else {
                $flag = 1;
            }
        }

        echo json_encode(['msg' => $msg, 'flag' => $flag,]);
    }


//  送卡记录
    public function actionSendGiftRecord()
    {
        $session = Yii::$app->session;
        $userinfo = $session->get(static::sessionKey);
        $myCards = T_WXMALL_DORACARD::get_OrderByUserid($userinfo->userid);
        foreach ($myCards as &$myCard) {
            $myCard['CREATE_DATETIME_SHOW'] = date("m-d", strtotime($myCard['CREATE_DATETIME']));
        }

//        $test='2016-01-09 13:05:49';
//        //echo date("Y-m-d H:i:s",time());
//        echo date("m-d",strtotime($test));
//        var_dump($myCards);
//        die;
        return $this->render("send-gift-record", [
            "myCards" => $myCards
        ]);
    }

    //显示已经购买的卡片信息 json
    public function actionShowBuyCardHandle()
    {
        $msg = "";
        $flag = 0;

        $request = Yii::$app->request;
        $orderid = $request->post("orderid");
        $cardid = $request->post("cardid");


        $count = T_WXMALL_DORACARD::get_orderCardCountByOrderId($orderid);
        $day_count = T_WXMALL_DORACARD::get_VALIDDaysByOrderId($orderid);
        $card = T_WXMALL_DORACARD::get_cardByCardId($cardid);
        if (count($card) == 0) {
            $msg = "没有此卡片";
        } else {
            $flag = 1;
            $card = $card[0];
        }
        //TO_DATE(end_datetime,'yyyy-mm-dd')-trunc(sysdate)
        echo json_encode(['msg' => $msg, 'flag' => $flag, 'count' => $count, 'card' => $card, 'day_count' => $day_count]);
    }

//我的卡片兑换记录
    public function actionSendGiftRecordChoice($status = 0)
    {
        $session = Yii::$app->session;
        $userinfo = $session->get(static::sessionKey);
        if ($status == 0) {
            $subsql = '1,2,3,4';
        } else {
            $subsql = $status;
        }
        $myCards = T_WXMALL_DORACARD::get_BagCardsByUserid($userinfo->userid, $subsql);

        foreach ($myCards as &$myCard) {
            $myCard['CREATE_DATETIME_SHOW'] = date("m-d", strtotime($myCard['CREATE_DATETIME']));
        }

        return $this->render("send-gift-record-choice", [
            "myCards" => $myCards, 'status' => $status
        ]);
    }

    //获取礼包领取情况  get-gift-receive-info-handle
    public function actionGetGiftReceiveInfoHandle()
    {

        $session = Yii::$app->session;
        $msg = "";
        $flag = 0;
        $request = Yii::$app->request;
        $order_code=$request->post('order_code');
        $userid = $session->get(static::sessionKey)->userid;

        $card = T_WXMALL_DORACARD::get_cardByOrderId($order_code);
        if (count($card) == 0) {
            echo "此卡片不存在！";
            echo json_encode(['msg' => $msg, 'flag' => $flag,]);
            exit;
        }
        $card = $card[0];



        $flag=1;
        $cardcodeid = T_WXMALL_DORACARD::isReceive_card($order_code, $userid);
        $isReceive = "0";
        if ($cardcodeid != false) {
            //echo "已经领取";
            $isReceive = "1";
        }
        $rCards = T_WXMALL_DORACARD::getReceiveCardListByOrderId($order_code);
        $count = T_WXMALL_DORACARD::get_orderCardCountByOrderId($order_code);
        $getcount = T_WXMALL_DORACARD::get_orderGetCardCountByOrderId($order_code);
        echo json_encode(['msg' => $msg, 'flag' => $flag,'isReceive' => $isReceive, 'card' => $card
            ,'rCards' => $rCards,  'count' => $count,
            'getcount' => $getcount, 'card_code_id'=>$cardcodeid]);
        exit;



    }
    // 领取卡片
    public function actionGiftReceive($id)
    {
        $session = Yii::$app->session;
        $card = T_WXMALL_DORACARD::get_cardByOrderId($id);
        if (count($card) == 0) {
            echo "此卡片不存在！";
            exit;
        }
        $card = $card[0];

        $userid = $session->get(static::sessionKey)->userid;
        $cardcodeid = T_WXMALL_DORACARD::isReceive_card($id, $userid);
        $isCardReceived = "false";
        if ($cardcodeid != false) {
            //echo "已经领取";
            //fix 已经兑换的卡片跳转
            $status = T_WXMALL_DORACARD::getGetStatusByCardCodeId($cardcodeid);
            if ($status == 2) {//已兑换
                return $this->redirect(["wx-biz/gift-exchange-detail", "id" => $cardcodeid, "status" => 2]);
            }
            $isCardReceived = "true";
        }

        $orderInfo = T_WXMALL_DORACARD::getOrderInfoByOrderCode_base($id);
        if (count($orderInfo) == 0) {
            echo '订单不存在';
            exit;
        }
        $orderInfo=$orderInfo[0];

        $user = T_WXMALL_USER::getUserByUserid($card['USER_ID']);
        $user = $user[0];



        return $this->render("gift-receive", ['card' => $card, 'order_code' => $id
            ,'orderInfo'=>$orderInfo
            , 'isCardReceived' => $isCardReceived, 'user' => $user]);
    }

    // 领取的卡片
    public function actionGiftReceiveDetail($id)
    {
        $info = $this->___getCardInfo($id);
        $orderid = $info->orderid;
        $count = T_WXMALL_DORACARD::get_orderCardCountByOrderId($orderid);
        $getcount = T_WXMALL_DORACARD::get_orderGetCardCountByOrderId($orderid);
        $rCards = T_WXMALL_DORACARD::getReceiveCardListByOrderId($orderid);

        $session = Yii::$app->session;
        $userid = $session->get(static::sessionKey)->userid;
        $m_user = T_WXMALL_USER::getUserByUserid($info->card['USER_ID']);
        if(count($m_user)==0){
            $m_user=null;
        }else{
            $m_user = $m_user[0];
        }


        return $this->render("gift-receive-detail", ['card' => $info->card, 'rCards' => $rCards, 'count' => $count,
            'getcount' => $getcount, 'order_id' => $orderid, 'card_code_id' => $id, 'user' => $m_user]);
    }

    // 兑换卡片详情
    public function actionGiftExchangeDetail($id, $status = 0)
    {
        $info = $this->___getCardInfo($id);

        $session = Yii::$app->session;
        $userid = $session->get(static::sessionKey)->userid;
        $m_user = T_WXMALL_USER::getUserByUserid($info->card['USER_ID']);
        if(count($m_user)==0){
            $m_user=null;
        }else{
            $m_user = $m_user[0];
        }

        if ($status == 2) {
           ;
            $good = T_WXMALL_DORACARD::get_GoodByCardCodeId($id);
            if (count($good) == 0) {
                echo "没有兑换的产品";
                exit;
            }
            $good = $good[0];
        }
        if ($status == 5) {
            $DORACOIN = HomeDao::get_DORACOIN($info->card["ORDER_ID"], $id, '1', $userid);
            if (count($DORACOIN) == 0) {
                echo "查询朵拉币失败";
                exit;
            }
            $DORACOIN = $DORACOIN[0];
            $good = HomeDao::get_CardInfo($id);
            if(count($good)>0){
                $good=$good[0];
            }
        }

        return $this->render("gift-exchange-detail", ['status'=>$status
            , 'good' => isset($good)?$good:null, 'card_code_id' => $id
            , 'user' =>$m_user,'blessing'=>$info->card['BLESSING'], 'doracoin'=>isset($DORACOIN)?$DORACOIN:null]);
    }

    //点击我的收礼记录跳转 查看礼物导航  code_id t_wxmall_dorabag_order_goods.id  view-gift-nav
    public function actionViewGiftNav($code_id)
    {
        $session = Yii::$app->session;
        $userid = $session->get(static::sessionKey)->userid;
        $status = T_WXMALL_DORACARD::getGetStatusByCardCodeId($code_id);
        if ($status == false) {
            echo "没有这个cardcodeid";
            exit;
        }
        if ($status == 1) {//未兑换
            return $this->redirect(["wx-biz/gift-receive-detail", "id" => $code_id]);
        }
        if ($status == 2) {//已兑换
            return $this->redirect(["wx-biz/gift-exchange-detail", "id" => $code_id, "status" => 2]);
        }
        if ($status == 3) {//已经过期
            echo "已经过期";
            exit;
        }
        if ($status == 4) {//已经失效
            echo "已经失效";
            exit;
        }
        if ($status == 5) {//已经失效
            return $this->redirect(["wx-biz/gift-exchange-detail", "id" => $code_id, "status" => 5]);
            exit;
        }
    }

    //兑换礼物收货信息
    public function actionGiftReceiverForm($good_code, $id,$address_id='')
    {
        $info = $this->___getCardInfo($id);
        $CARD_ID = $info->card['ID'];
        $good = T_WXMALL_DORACARD::get_goodByCardIdAndGoodId($CARD_ID, $good_code);
        if (count($good) == 0) {
            echo "此产品不存在！";
            exit;
        }
        $good = $good[0];
        $IMG_INFO = BagDao::get_GOODS_IMG_INFO($good['GOODS_CODE']);
        //获取地址
        $address=HomeDao::getUserAddress($address_id,static::getSessionUserid());

        return $this->render("gift-receiver-form", ['good' => $good, 'IMG_INFO' => $IMG_INFO, 'card_id' => $CARD_ID,
             'card_code_id' => $id,
            'address'=>$address,
        ]);
    }
}